<template>
  <div class="manage">
    <el-dialog
      :title="operateType === 'add' ? '新增用户' : '重命名'"
      :visible.sync="isShow"
    >
      <common-form
        :formLabel="operateFormLabel"
        :form="operateForm"
        ref="form"
      ></common-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>

    <div class="manage-header">
          <el-select v-model="value" placeholder="请选择需要导出的文书类型">
      <el-option
        v-for="item in options"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      >
      </el-option>
    </el-select>

      <common-form inline :formLabel="formLabel" :form="searchFrom">
        <el-button
          type="primary"
          @click="getList(searchFrom.keyword)"
          size="primary"
          >搜索</el-button
        >
      </common-form>
    </div>

    <common-table-document
      :tableData="tableData"
      :tableLabel="tableLabel"
      :config="config"
      @changePage="getList()"
      @edit="editUser"
      @del="delUser"
      @excel="exportExcel"
      @word="exportWord"
      @zip="exportZip"
    ></common-table-document>
  </div>
</template>

<script>
import CommonForm from "@/components/CommonForm";

import CommonTableDocument from "@/components/CommonTableDocument";
import { findByPageApi } from "@/api/getHouseLoanBatchData.js";
import { deleteByIdApi } from "@/api/getHouseLoanBatchData.js";
import { updateAPi } from "@/api/getHouseLoanBatchData.js";
import { findByName } from "@/api/getHouseLoanBatchData.js";
import { exportExcel } from "@/api/getHouseLoanBatchData.js";
import { exportWord } from "@/api/getHouseLoanBatchData.js";
import { exportZip } from "@/api/getHouseLoanBatchData.js";
import { exportWord_1 } from "@/api/getHouseLoanBatchData.js";
import { exportZip_1 } from "@/api/getHouseLoanBatchData.js";
import { exportWord_2 } from "@/api/getHouseLoanBatchData.js";
import { exportZip_2 } from "@/api/getHouseLoanBatchData.js";
import { exportWord_3 } from "@/api/getHouseLoanBatchData.js";
import { exportZip_3 } from "@/api/getHouseLoanBatchData.js";


export default {
  components: {
    CommonForm,
    CommonTableDocument,
  },
  data() {
    return {
      operateType: "add",
      isShow: false, //信息修改消息框
      tableData: [],
      tableLabel: [
        {
          prop: "name",
          label: "批次名称",
          required: true,
        },

        {
          prop: "key",
          label: "唯一标识",
          required: true,
        },
        {
          prop: "date",
          label: "更新日期",
          required: true,
        },
      ],
      config: {
        page: 1,
        total: 30,
        loading: false,
      },
      operateForm: {
        id: "",
        name: "",
        key: "",
      },
      operateFormLabel: [
        {
          model: "name",
          label: "批次名称",
        },
        {
          model: "key",
          label: "唯一标识",
        },
      ],

      searchFrom: {
        keyword: "",
      },
      formLabel: [
        {
          model: "keyword",
          label: "",
        },
      ],

      options: [
        {
          value: 0,
          label: "强制执行申请书-房贷",
        },
        {
          value: 1,
          label: "民事起诉状-房贷-个人有抵押无担保",
        },
        {
          value: 2,
          label:"民事起诉状-房贷-个人有抵押有担保",

        },
        {
          value:3,
          label:"民事起诉状-房贷-夫妻债务有抵押无担保"

        }

      ],
      value: 0,
    };
  },
  methods: {
    async getList(name) {
      this.config.loading = false;
      // 搜索时，页码需要设置为1，才能正确返回数据，因为数据是从第一页开始返回的
      name ? (this.config.page = 1) : "";
      if (this.searchFrom.keyword == "") {
        const result = await findByPageApi(this.config.page);
        console.log(result);
        if (result.data.code == 0) {
          this.tableData = result.data.data.houseLoanBatches;
          this.config.total = result.data.data.count;
          this.config.loading = false;
        }
      } else {
        console.log(name);
        const result = await findByName(
          this.searchFrom.keyword,
          this.config.page
        );
        console.log(result);

        this.tableData = result.data.data.houseLoanBatches;
        this.config.total = result.data.data.count;
        this.config.loading = false;
      }
    },
    addUser() {
      this.operateForm = {};
      this.operateType = "add";
      this.isShow = true;
    },
    editUser(row) {
      this.operateType = "edit";
      this.isShow = true;
      this.operateForm = row;
    },
    exportExcel(row) {
      exportExcel(row.name, row.key).then((res) => {
        let blob = new Blob([res.data], {
          type: "application/vnd.ms-excel;charset=UTF-8",
        }); //规定文件类型 res就是返回你的数据流了
        let link = document.createElement("a");
        // link.download = fileName//a标签添加属性
        let objectUrl = URL.createObjectURL(blob);
        link.setAttribute("href", objectUrl);

        link.setAttribute("download", row.name);
        link.click();
        //释放内存
        window.URL.revokeObjectURL(link.href);
      });
    },
    exportZip(row) {
      console.log(this.value)
      if(this.value == 0)
      {
      console.log(row.key + "zip");
      exportZip(row.name, row.key).then((res) => {
        let blob = new Blob([res.data], {
          type: "aplication/zip;charset=UTF-8",
        }); //规定文件类型 res就是返回你的数据流了
        let link = document.createElement("a");
        // link.download = fileName//a标签添加属性
        let objectUrl = URL.createObjectURL(blob);
        link.setAttribute("href", objectUrl);
        link.setAttribute("download", row.name +"强制执行申请书-房贷"+ ".zip");
        link.click();
        //释放内存
        window.URL.revokeObjectURL(link.href);
      });

      }
      if(this.value == 1)
      {
      console.log(row.key + "zip");
      exportZip_1(row.name, row.key).then((res) => {
        let blob = new Blob([res.data], {
          type: "aplication/zip;charset=UTF-8",
        }); //规定文件类型 res就是返回你的数据流了
        let link = document.createElement("a");
        // link.download = fileName//a标签添加属性
        let objectUrl = URL.createObjectURL(blob);
        link.setAttribute("href", objectUrl);
        link.setAttribute("download", row.name +"民事起诉状-房贷-个人有抵押无担保"+ ".zip");
        link.click();
        //释放内存
        window.URL.revokeObjectURL(link.href);
      });

      }
      if(this.value == 2)
      {

              console.log(row.key + "zip");
      exportZip_2(row.name, row.key).then((res) => {
        let blob = new Blob([res.data], {
          type: "aplication/zip;charset=UTF-8",
        }); //规定文件类型 res就是返回你的数据流了
        let link = document.createElement("a");
        // link.download = fileName//a标签添加属性
        let objectUrl = URL.createObjectURL(blob);
        link.setAttribute("href", objectUrl);
        link.setAttribute("download", row.name +"民事起诉状-房贷-个人有抵押有担保"+ ".zip");
        link.click();
        //释放内存
        window.URL.revokeObjectURL(link.href);
      });

      }
      if(this.value == 3)
      {

              console.log(row.key + "zip");
      exportZip_3(row.name, row.key).then((res) => {
        let blob = new Blob([res.data], {
          type: "aplication/zip;charset=UTF-8",
        }); //规定文件类型 res就是返回你的数据流了
        let link = document.createElement("a");
        // link.download = fileName//a标签添加属性
        let objectUrl = URL.createObjectURL(blob);
        link.setAttribute("href", objectUrl);
        link.setAttribute("download", row.name +"民事起诉状-房贷-夫妻债务有抵押无担保"+ ".zip");
        link.click();
        //释放内存
        window.URL.revokeObjectURL(link.href);
      });

      }

    },
    exportWord(row) {
      console.log("exportWord");

      console.log(row.key + "word");
      if(this.value == 0 )
      {
        exportWord(row.name, row.key).then((res) => {
        let blob = new Blob([res.data], {
          type: "application/msword;charset=UTF-8",
        }); //规定文件类型 res就是返回你的数据流了
        let link = document.createElement("a");
        // link.download = fileName//a标签添加属性
        let objectUrl = URL.createObjectURL(blob);
        link.setAttribute("href", objectUrl);

        link.setAttribute("download", row.name+"强制执行申请书-房贷");
        link.click();
        //释放内存
        window.URL.revokeObjectURL(link.href);
      });

      }
      if(this.value == 1)
      {
        exportWord_1(row.name, row.key).then((res) => {
        let blob = new Blob([res.data], {
          type: "application/msword;charset=UTF-8",
        }); //规定文件类型 res就是返回你的数据流了
        let link = document.createElement("a");
        // link.download = fileName//a标签添加属性
        let objectUrl = URL.createObjectURL(blob);
        link.setAttribute("href", objectUrl);

        link.setAttribute("download", row.name+"民事起诉状-房贷-个人有抵押无担保");
        link.click();
        //释放内存
        window.URL.revokeObjectURL(link.href);
      });

      }

      if(this.value == 2)
      {

        exportWord_2(row.name, row.key).then((res) => {
        let blob = new Blob([res.data], {
          type: "application/msword;charset=UTF-8",
        }); //规定文件类型 res就是返回你的数据流了
        let link = document.createElement("a");
        // link.download = fileName//a标签添加属性
        let objectUrl = URL.createObjectURL(blob);
        link.setAttribute("href", objectUrl);

        link.setAttribute("download", row.name+"民事起诉状-房贷-个人有抵押有担保");
        link.click();
        //释放内存
        window.URL.revokeObjectURL(link.href);
      });

      }
      if(this.value == 3)
      {

                exportWord_3(row.name, row.key).then((res) => {
        let blob = new Blob([res.data], {
          type: "application/msword;charset=UTF-8",
        }); //规定文件类型 res就是返回你的数据流了
        let link = document.createElement("a");
        // link.download = fileName//a标签添加属性
        let objectUrl = URL.createObjectURL(blob);
        link.setAttribute("href", objectUrl);

        link.setAttribute("download", row.name+"民事起诉状-房贷-夫妻债务有抵押无担保");
        link.click();
        //释放内存
        window.URL.revokeObjectURL(link.href);
      });

      }


    },
    confirm() {
      if (this.operateType === "edit") {
        updateAPi(
          //       id, party, lawsuit_num, execute_num, sex, birth_date,
          // nation, card_num, tel, home_address, current_address,
          // work_address, principal, interest, amount, litigation_fee,
          // declared_fee, lawyer_fee, credit_card, deadline_date, open_date,
          // last_consume_date, last_pay_date, start_over_date, judge_principal, judge_interest,
          // court_location, paper_sign_date, course

          this.operateForm.id,
          this.operateForm.name
        ).then((res) => {
          (this.isShow = false),
            this.$message({
              type: "success",
              message: "编辑成功!",
            });
          this.getList();
        });
      } else {
        //此模块不需要新增功能
      }
    },

    delUser(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let id = row.id;
          // this.$http
          //   .get('/api/v1/pub/defendant_one/deleteById', {
          //     params: {
          //       id
          //     }
          //   })
          const result = deleteByIdApi(id).then((res) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  created() {
    this.getList();
  },
};
</script>
<style lang="scss" scoped>
.manage {
  height: 90%;
  padding-bottom: 20px;
  padding-top: 20px;
  overflow: hidden;
  &-header {
    display: flex;
    // display: inline-block;
    justify-content: space-between;
    align-items: flex-start;
  }
}
</style>